﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using net.xBei.Models.json;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace net.xBei.WebApi.Helper
{
    /// <summary>
    /// 
    /// </summary>
    public static class ControllerHelper
    {
        /// <summary>
        /// 从Context中获取用户ID
        /// </summary>
        /// <param name="httpContext"></param>
        /// <returns></returns>
        public static int GetCurrentUserId(this HttpContext httpContext) {
            if (httpContext != null 
                    && httpContext.User != null 
                    && httpContext.User.Identity != null 
                    && !string.IsNullOrWhiteSpace(httpContext.User.Identity.Name)) {
                var userIdString = httpContext.User.Identity.Name;
                //Console.WriteLine($"GetCurrentUserId {userIdString}");
                if (int.TryParse(userIdString, out int userId)) {
                    return userId;
                }
            }
            return -1;
        }
        /// <summary>
        /// 从Context.User中获取用户ID
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static int GetCurrentUserId(this ClaimsPrincipal user) {
            if (user != null && user.Identity != null && !string.IsNullOrEmpty(user.Identity.Name)) {
                var userIdString = user.Identity.Name;
                Console.WriteLine($"GetCurrentUserId {userIdString}");
                if (int.TryParse(userIdString, out int userId))
                    return userId;
            }
            return -1;
        }
        /// <summary>
        /// 标准返回
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="controller"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static IActionResult Success<T>(this ControllerBase controller, T result) {
            if (null == result) {
                return controller.StatusCode((int)HttpStatusCode.InternalServerError, new { Success = false, Message = "服务错误" });
            }
            return controller.Ok(new {
                Success = true,
                Data = result,
            });
        }
        /// <summary>
        /// 返回
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="controller"></param>
        /// <param name="response"></param>
        /// <returns></returns>
        public static IActionResult GetActionResult<T>(this ControllerBase controller, ResponseData<T> response) {
            if (response == null) {
                return Error(controller, "内部错误", 500);
            }
            if(response.Success== false) {
                return Error(controller, response.Message);
            }
            return controller.Ok(response);
        }
        /// <summary>
        /// 错误返回
        /// </summary>
        /// <param name="controller"></param>
        /// <param name="message"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static IActionResult Error(this ControllerBase controller, string message, int code = 200) {
            return code switch {
                200 => controller.Ok(new {
                    Success = false,
                    Message = message,
                }),
                400 => controller.StatusCode(
                    (int)HttpStatusCode.BadRequest, new {
                        Success = false,
                        Message = message,
                    }),
                401 => controller.StatusCode(
                        (int)HttpStatusCode.Unauthorized, new {
                            Success = false,
                            Message = message,
                        }),
                403 => controller.StatusCode(
                        (int)HttpStatusCode.Forbidden, new {
                            Success = false,
                            Message = message,
                        }),
                _ => controller.StatusCode(
                        (int)HttpStatusCode.InternalServerError,
                        new {
                            Success = false,
                            Message = $"错误的code={code}"
                        }),
            };
        }
    }
}
